<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : '编辑'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    class="jsbos-dialog jsbos-dialog_center"
    lock-scroll
    width="800px"
  >
    <el-row :gutter="15" class="">
      <el-form
        ref="elForm"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="50px"
        label-position="right"
      >
        <template v-if="!loading">
          <el-col :span="24">
            <el-form-item label="类型" prop="category">
              <el-select
                v-model="dataForm.category"
                placeholder="请选择"
                clearable
                :style="{ width: '100%' }"
                @change="categoryChange"
              >
                <el-option
                  v-for="(item, index) in categoryOptions"
                  :key="index"
                  :label="item.fullName"
                  :value="item.enCode"
                  :disabled="item.disabled"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="名称" prop="fullName">
              <el-input
                v-model="dataForm.fullName"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="编码" prop="enCode">
              <el-input
                v-model="dataForm.enCode"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label-width="0">
              <div class="jsbos-common-title">
                <h2>关键词</h2>
              </div>
              <el-table :data="dataForm.keywordList" size="mini">
                <el-table-column
                  type="index"
                  width="50"
                  label="序号"
                  :align="'center'"
                />
                <el-table-column prop="enCode" label="参数名">
                  <template slot="header">
                    <span class="required-sign">*</span>参数名
                  </template>
                  <template slot-scope="scope">
                    <el-input
                      :readonly="!scope.row.closable"
                      v-model="scope.row.enCode"
                      placeholder="请输入"
                      clearable
                      :style="{ width: '100%' }"
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="fullName" label="注释">
                  <template slot="header">
                    <span class="required-sign">*</span>注释
                  </template>
                  <template slot-scope="scope">
                    <el-input
                      :readonly="!scope.row.closable"
                      v-model="scope.row.fullName"
                      placeholder="请输入"
                      clearable
                      :style="{ width: '100%' }"
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="example" label="示例">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.example"
                      placeholder="请输入"
                      clearable
                      :style="{ width: '100%' }"
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="50">
                  <template slot-scope="scope">
                    <el-button
                      v-show="scope.row.closable"
                      size="mini"
                      type="text"
                      class="jsbos-table-delBtn"
                      @click="delKeywordList(scope.$index)"
                      >删除</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
              <div class="table-actions" @click="addKeywordList()">
                <el-button type="text" icon="el-icon-plus">添加</el-button>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态" prop="enabledMark">
              <el-switch
                v-model="dataForm.enabledMark"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述" prop="description">
              <el-input
                v-model="dataForm.description"
                placeholder="请输入"
                :style="{ width: '100%' }"
                true
                type="textarea"
                :autosize="{ minRows: 4, maxRows: 4 }"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="btnLoading" @click="visible = false">
        取 消</el-button
      >
      <el-button :loading="btnLoading" type="primary" @click="dataFormSubmit()">
        确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import { getInfo, Update, Create } from "@/views/message/api/mcBusiness";
export default {
  components: {},
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      btnLoading: false,
      dataForm: {
        category: "textmsg",
        fullName: "",
        enCode: "",
        enabledMark: 1,
        description: "",
        keywordList: [],
      },
      rules: {
        category: [
          {
            required: true,
            message: "请至少选择一个",
            trigger: "change",
          },
        ],
        fullName: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        enCode: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        enabledMark: [
          {
            required: true,
            message: "请至少选择一个",
            trigger: "change",
          },
        ],
      },
      categoryOptions: [],

      textmsgKeyword: [],
      linkmsgKeyword: [
        {
          fullName: "消息链接",
          enCode: "linkUrl",
          example: undefined,
          closable: false,
        },
      ],
      imgtextmsgKeyword: [
        {
          fullName: "图片地址",
          enCode: "picUrl",
          example: undefined,
          closable: false,
        },
      ],
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getcategoryOptions();
  },
  mounted() {},
  methods: {
    categoryChange(val) {
      console.log(val);
      if (val) {
        if (val == "textmsg") {
          this.dataForm.keywordList = this.textmsgKeyword;
        }
        if (val == "linkmsg") {
          this.dataForm.keywordList = this.linkmsgKeyword;
        }
        if (val == "imgtextmsg") {
          this.dataForm.keywordList = this.imgtextmsgKeyword;
        }
      }
    },
    mcbusinesskeywordExist() {
      let isOk = true;
      for (let i = 0; i < this.dataForm.keywordList.length; i++) {
        const e = this.dataForm.keywordList[i];
        if (!e.fullName) {
          this.$message({
            message: "名称不能为空",
            type: "error",
            duration: 1000,
          });
          isOk = false;
          break;
        }
        if (!e.enCode) {
          this.$message({
            message: "编码不能为空",
            type: "error",
            duration: 1000,
          });
          isOk = false;
          break;
        }
      }
      return isOk;
    },
    getcategoryOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "msgType",
        })
        .then((res) => {
          this.categoryOptions = res;
        });
    },
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          getInfo(this.dataForm.id).then((res) => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } else {
          this.clearData(this.dataForm);
          this.dataForm.category = "textmsg";
          this.dataForm.enabledMark = 1;
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          if (!this.mcbusinesskeywordExist()) return;

          var _data = this.dataList();
          this.btnLoading = true;
          const formMethod = this.dataForm.id ? Update : Create;
          formMethod(_data)
            .then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.visible = false;
                  this.btnLoading = false;
                  this.$emit("refresh", true);
                },
              });
            })
            .catch(() => {
              this.btnLoading = false;
            });
        }
      });
    },
    addKeywordList() {
      let item = {
        fullName: undefined,
        enCode: undefined,
        example: undefined,
        closable: true,
      };
      this.dataForm.keywordList.push(item);
    },
    delKeywordList(index) {
      this.dataForm.keywordList.splice(index, 1);
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.enabledMark = parseInt(_data.enabledMark);
      for (let i = 0; i < _data.keywordList.length; i++) {
        var _list = _data.keywordList[i];
      }
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.enabledMark = parseInt(_dataAll.enabledMark);
      for (let i = 0; i < _dataAll.keywordList.length; i++) {
        var _list = _dataAll.keywordList[i];
      }
      this.dataForm = _dataAll;
    },
  },
};
</script>
